{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sum of Digits of String After Convert"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: getLucky"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串转化后的各位数字之和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个由小写字母组成的字符串 <code>s</code> ，以及一个整数 <code>k</code> 。</p>\n",
    "\n",
    "<p>首先，用字母在字母表中的位置替换该字母，将 <code>s</code> <strong>转化</strong> 为一个整数（也就是，<code>'a'</code> 用 <code>1</code> 替换，<code>'b'</code> 用 <code>2</code> 替换，... <code>'z'</code> 用 <code>26</code> 替换）。接着，将整数 <strong>转换</strong> 为其 <strong>各位数字之和</strong> 。共重复 <strong>转换</strong> 操作 <strong><code>k</code> 次</strong> 。</p>\n",
    "\n",
    "<p>例如，如果 <code>s = \"zbax\"</code> 且 <code>k = 2</code> ，那么执行下述步骤后得到的结果是整数 <code>8</code> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><strong>转化：</strong><code>\"zbax\" ➝ \"(26)(2)(1)(24)\" ➝ \"262124\" ➝ 262124</code></li>\n",
    "\t<li><strong>转换 #1</strong>：<code>262124&nbsp;➝ 2 + 6 + 2 + 1 + 2 + 4&nbsp;➝ 17</code></li>\n",
    "\t<li><strong>转换 #2</strong>：<code>17 ➝ 1 + 7 ➝ 8</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>返回执行上述操作后得到的结果整数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"iiii\", k = 1\n",
    "<strong>输出：</strong>36\n",
    "<strong>解释：</strong>操作如下：\n",
    "- 转化：\"iiii\" ➝ \"(9)(9)(9)(9)\" ➝ \"9999\" ➝ 9999\n",
    "- 转换 #1：9999 ➝ 9 + 9 + 9 + 9 ➝ 36\n",
    "因此，结果整数为 36 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"leetcode\", k = 2\n",
    "<strong>输出：</strong>6\n",
    "<strong>解释：</strong>操作如下：\n",
    "- 转化：\"leetcode\" ➝ \"(12)(5)(5)(20)(3)(15)(4)(5)\" ➝ \"12552031545\" ➝ 12552031545\n",
    "- 转换 #1：12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33\n",
    "- 转换 #2：33 ➝ 3 + 3 ➝ 6\n",
    "因此，结果整数为 6 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= k &lt;= 10</code></li>\n",
    "\t<li><code>s</code> 由小写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sum-of-digits-of-string-after-convert](https://leetcode.cn/problems/sum-of-digits-of-string-after-convert/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sum-of-digits-of-string-after-convert](https://leetcode.cn/problems/sum-of-digits-of-string-after-convert/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"iiii\"\\n1', '\"leetcode\"\\n2', '\"zbax\"\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "\n",
    "        count=0\n",
    "        for i in range(len(s)):\n",
    "            for x in str(ord(s[i])-ord(\"a\")+1):\n",
    "                count+=int(x)\n",
    "        while k-1>0:\n",
    "            tmp=count\n",
    "            count=0\n",
    "            for i in str(tmp):\n",
    "                count+=int(i)\n",
    "            k-=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        dic = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8]\n",
    "        ans = 0\n",
    "        for c in s:\n",
    "            ans += dic[(ord(c) - 97)]\n",
    "        while k > 1:\n",
    "            ans = sum([int(i) for i in list(str(ans))])\n",
    "            k -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        def zhuanhuan(s):\n",
    "            m=\"\"\n",
    "            for i in s:\n",
    "                m=m+str(ord(i)-ord('a')+1)\n",
    "            return m\n",
    "        def xiangjia(s):\n",
    "            s =str(s)\n",
    "            res = 0\n",
    "            for i in s:\n",
    "                res = res +int(i)\n",
    "            return res\n",
    "        s = zhuanhuan(s)\n",
    "\n",
    "        while(k!=0):\n",
    "            s = xiangjia(s)\n",
    "            k=k-1\n",
    "        return (s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        list = []\n",
    "        for i in s:\n",
    "            for x in str(ord(i)-96):\n",
    "                list.append(int(x))\n",
    "        for x in range(k):\n",
    "            re = sum(list)\n",
    "            list = []\n",
    "            for i in str(re):\n",
    "                list.append(int(i))\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "      s=''.join(str(ord(c)-ord('a')+1)for c in s)\n",
    "      for i in range(k):\n",
    "        t=sum(int(c)for c in s)\n",
    "        s=str(t)\n",
    "      return int(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        s = ''.join(str(ord(c) - ord('a') + 1) for c in s)\n",
    "        for _ in range(k):\n",
    "            t = sum(int(c) for c in s)\n",
    "            s = str(t)\n",
    "        return int(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        def conversion(s: str, k: int):\n",
    "            print(s)\n",
    "            y = sum([int(n) for n in s])\n",
    "            print(y)\n",
    "            if k == 0:\n",
    "                return y\n",
    "            return conversion(s=''.join([n for n in str(y)]), k=k-1)\n",
    "        return conversion(s=''.join([str(ord(num) - 96) for num in s]), k=k-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        tmp = \"\"\n",
    "        for i in s:\n",
    "            tmp += str(ord(i) - ord(\"a\") + 1)\n",
    "        for j in range(k):\n",
    "            tmp_1 = tmp\n",
    "            tmp = str(sum([int(x) for x in tmp_1]))\n",
    "        return int(tmp)\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        tmp = ''\n",
    "        for i in s:\n",
    "            tmp += str(ord(i)-96)\n",
    "        n = int(tmp)\n",
    "        for _ in range(k):\n",
    "            res = 0\n",
    "            while n:\n",
    "                res += n%10\n",
    "                n //= 10\n",
    "            n = res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        # 纯模拟而已\n",
    "        s1 = ''.join(str(ord(c)-ord('a')+1) for c in s)\n",
    "        while k:\n",
    "            s1 = str(sum(int(c) for c in s1))\n",
    "            k -= 1\n",
    "        return int(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        tmp = ''\n",
    "        for i in s:\n",
    "            tmp += str(ord(i)-96)\n",
    "        n = int(tmp)\n",
    "        for _ in range(k):\n",
    "            res = 0\n",
    "            while n:\n",
    "                res += n%10\n",
    "                n //= 10\n",
    "            n = res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "\n",
    "        count=0\n",
    "        for i in range(len(s)):\n",
    "            for x in str(ord(s[i])-ord(\"a\")+1):\n",
    "                count+=int(x)\n",
    "        while k-1>0:\n",
    "            tmp=count\n",
    "            count=0\n",
    "            for i in str(tmp):\n",
    "                count+=int(i)\n",
    "            k-=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        s = list(s)\n",
    "        temp = \"\"\n",
    "        for i in s:\n",
    "            temp += str(ord(i)-ord(\"a\")+1)\n",
    "        temp = int(temp)\n",
    "        def calculate(num):\n",
    "            res = 0\n",
    "            while num>=10:\n",
    "                res += num%10\n",
    "                num = num //10\n",
    "            res += num\n",
    "            return res\n",
    "        print(temp)\n",
    "        for i in range(k):\n",
    "            temp = calculate(temp)\n",
    "        return temp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        res = ''\n",
    "        for ch in s:\n",
    "            res += str(ord(ch) - ord('a') + 1)\n",
    "        while k:\n",
    "            res = sum(int(ch) for ch in res)\n",
    "            res = str(res)\n",
    "            k -= 1\n",
    "\n",
    "        return int(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        lucky = ''\n",
    "        for c in s:\n",
    "            lucky += str(ord(c) - ord('a') + 1)\n",
    "        while k > 0:\n",
    "            k -= 1\n",
    "            lucky = str(sum([int(c) for c in lucky]))\n",
    "        return int(lucky)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        l = []\n",
    "        for c in s:\n",
    "            l.append(str(ord(c) - 96))\n",
    "        temp = ''.join(l)\n",
    "        for _ in range(k):\n",
    "            temp = str(sum(int(c) for c in temp))\n",
    "        return int(temp)\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        az = {\"a\":\"1\", \"b\":\"2\", \"c\":\"3\", \"d\":\"4\", \"e\":\"5\", \"f\":\"6\", \"g\":\"7\", \"h\":\"8\", \"i\":\"9\", \"j\":\"10\", \"k\":\"11\", \"l\":\"12\", \"m\":\"13\", \"n\":\"14\", \"o\":\"15\", \"p\":\"16\", \"q\":\"17\", \"r\":\"18\", \"s\":\"19\", \"t\":\"20\", \"u\":\"21\", \"v\":\"22\", \"w\":\"23\", \"x\":\"24\", \"y\":\"25\", \"z\":\"26\"}\n",
    "        t = \"\"\n",
    "        r = 0\n",
    "        for i in s:\n",
    "            t += az[i]\n",
    "        for j in range(k):\n",
    "            r = 0\n",
    "            for n in t:\n",
    "                r += int(n)\n",
    "            t = str(r)\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        y = 0\n",
    "        for i in s:\n",
    "            i = ord(i) - 96\n",
    "            if i > 9:\n",
    "                y += sum(divmod(i, 10))\n",
    "            else:\n",
    "                y += i\n",
    "        x = y\n",
    "        for _ in range(k-1):\n",
    "            x = 0\n",
    "            while y:\n",
    "                x += y%10\n",
    "                y //= 10\n",
    "            y = x\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        digits = \"\".join(str(ord(ch) - ord(\"a\") + 1) for ch in s)\n",
    "        for i in range(k):\n",
    "            if len(digits) == 1:\n",
    "                break\n",
    "            total = sum(int(ch) for ch in digits)\n",
    "            digits = str(total)\n",
    "        return int(digits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def getLucky(self, s: str, k: int) -> int:\n",
    "    list1 =''\n",
    "    for i in s:\n",
    "      list1 +=str(ord(i) - ord('a') + 1)\n",
    "    while k:\n",
    "      ans = 0\n",
    "      for x in list1:\n",
    "        ans += int(x)\n",
    "      list1 = [int(i) for i in str(ans)]\n",
    "      k -= 1\n",
    "    return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        s1 = \"\"\n",
    "        for i in s:\n",
    "            s1 += str(ord(i)-ord(\"a\")+1)\n",
    "        while k>0:\n",
    "            b=0\n",
    "            for j in s1:\n",
    "                b += int(j)\n",
    "            s1 = str(b)\n",
    "            k -= 1\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        ns = int(''.join(map(lambda x: str(ord(x) - 96), list(s))))\n",
    "        \n",
    "        for i in range(k):\n",
    "            temp = 0\n",
    "            while ns:\n",
    "                temp += ns % 10\n",
    "                ns //= 10\n",
    "            ns = temp\n",
    "           \n",
    "                \n",
    "        return temp\n",
    "           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        nums = \"\"\n",
    "        for ss in s:\n",
    "            nums += str(ord(ss) - ord('a') + 1)\n",
    "        for _ in range(k):\n",
    "            after = 0\n",
    "            for n in nums:\n",
    "                after += int(n)\n",
    "            nums = str(after)\n",
    "        return int(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        ns = ''.join(map(lambda x: str(ord(x) - 96), list(s)))\n",
    "        \n",
    "        for i in range(k):\n",
    "            ns = str(sum(map(lambda x: int(x), list(ns))))\n",
    "        return int(ns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        s = ''.join(str(ord(c) - ord('a') + 1) for c in s)\n",
    "        for _ in range(k):\n",
    "            t = sum(int(c) for c in s)\n",
    "            s = str(t)\n",
    "        return int(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        letters = [chr(i) for i in range(97, 123)]\n",
    "        alpha = ''.join(letters)\n",
    "\n",
    "        nums = \"\"\n",
    "        for i in s:\n",
    "            nums += str(alpha.index(i) + 1)\n",
    "\n",
    "        for i in range(k):\n",
    "            temp = 0\n",
    "            for i in nums:\n",
    "                temp += int(i)\n",
    "            \n",
    "            nums = str(temp)\n",
    "        return int(nums)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        num_list = []\n",
    "        for i in s:\n",
    "            for x in str(ord(i)-96):\n",
    "                num_list.append(int(x))\n",
    "        for _ in range(k):\n",
    "            y = sum(num_list)\n",
    "            num_list = []\n",
    "            for x in str(y):\n",
    "                num_list.append(int(x))\n",
    "        return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def getLucky(self, s: str, k: int) -> int:\r\n",
    "        def to_number(s: str) -> int:\r\n",
    "            return int(\"\".join([str(ord(c) - 96) for c in s]))\r\n",
    "        \r\n",
    "        def bit_sum(x:int) -> int:\r\n",
    "            total = 0\r\n",
    "            while x:\r\n",
    "                total += x % 10\r\n",
    "                x //= 10\r\n",
    "\r\n",
    "            return total\r\n",
    "\r\n",
    "        n = to_number(s)\r\n",
    "        for _ in range(k):\r\n",
    "            n = bit_sum(n)\r\n",
    "        \r\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        ans = 0\n",
    "        for ch in s:\n",
    "            num = ord(ch) - 96\n",
    "            while num:\n",
    "                ans += num % 10\n",
    "                num //= 10\n",
    "        k -= 1\n",
    "        # 此时已经转换了一次(以防直接转换后的数字长度越界，也便于存储与后续的转换)\n",
    "        \n",
    "        while k:\n",
    "            res = 0\n",
    "            while ans:\n",
    "                res += ans % 10\n",
    "                ans //= 10\n",
    "            ans = res\n",
    "            k -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        ans = []\n",
    "        for ch in s:\n",
    "            ans.append(str(ord(ch)-ord('a')+1))\n",
    "        ans = list(''.join(ans))\n",
    "        ans = [int(i) for i in ans]\n",
    "        while k > 0:\n",
    "            res = sum(ans)\n",
    "            ans = [int(i) for i in list(str(res))]\n",
    "            k -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import string\n",
    "\n",
    "letters = string.ascii_lowercase\n",
    "# print(letters)\n",
    "litter_dict = {item: str(index + 1) for index, item in enumerate(letters)}\n",
    "# print(litter_dict)\n",
    "\n",
    "\n",
    "def recursion(n_s, k):\n",
    "    if k == 0:\n",
    "        return int(n_s)\n",
    "    return recursion(str(sum([int(i) for i in list(n_s)])), k - 1)\n",
    "\n",
    "class Solution:\n",
    "        \n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        num_s = ''\n",
    "        for item in s:\n",
    "            num_s += litter_dict.get(item)\n",
    "        return recursion(num_s, k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        res=\"\"\n",
    "        for char in s:\n",
    "            res+=str(ord(char)-ord(\"a\")+1)\n",
    "\n",
    "\n",
    "\n",
    "        while k:\n",
    "            ans = 0\n",
    "            for char in res:\n",
    "                ans+=int(char)\n",
    "            res=str(ans)\n",
    "            k-=1\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        ret = ''\n",
    "        for ch in s:\n",
    "            ret += str(ord(ch)-ord('a')+1)\n",
    "        while k:\n",
    "            temp = 0\n",
    "            for num in ret:\n",
    "                temp += int(num)\n",
    "            k -= 1\n",
    "            if k == 0:\n",
    "                return temp\n",
    "            ret = str(temp)\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        l = len(s)\n",
    "        h = 0\n",
    "        for i in range(l):\n",
    "            expr = ord(s[i]) - 96\n",
    "            if len(str(expr)) == 1:\n",
    "                h *= 10\n",
    "            else:\n",
    "                h *= 100\n",
    "            h += expr\n",
    "        m = 1\n",
    "        while m <= k:\n",
    "            sum = 0\n",
    "            for i in range(len(str(h))):\n",
    "                sum += int(str(h)[i])\n",
    "            h = sum\n",
    "            m += 1\n",
    "        return sum\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        l = [str(ord(x) - 96) for x in s]\n",
    "        l = ''.join(l)\n",
    "        while k > 0:\n",
    "            res = 0\n",
    "            for x in l:\n",
    "                res += int(x)\n",
    "            l = str(res)\n",
    "            k -= 1\n",
    "        return int(l)\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        str2Int = []\n",
    "        for i in s:\n",
    "            str2Int.append(str(ord(i)-ord('a')+1))\n",
    "        intNum = int(''.join(str2Int))\n",
    "        for j in range(k):\n",
    "            temp = []\n",
    "            while intNum >= 10:\n",
    "                temp.append(intNum%10)\n",
    "                intNum = intNum // 10\n",
    "            intNum = sum(temp)+intNum\n",
    "            if intNum < 10:\n",
    "                return intNum\n",
    "        return intNum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        num = \"\".join(str(ord(x) - ord('a') + 1) for x in s)\n",
    "        for i in range(k):\n",
    "            tot = sum(int(x) for x in num)\n",
    "            num = str(tot)\n",
    "        return int(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        ns = ''.join(map(lambda x: str(ord(x) - 96), list(s)))\n",
    "        \n",
    "        for i in range(k):\n",
    "            ns = str(sum(map(lambda x: int(x), list(ns))))\n",
    "        return int(ns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        ss=''\n",
    "        temp=0\n",
    "        for i in s:\n",
    "            ss+=str(ord(i)-ord('a')+1)\n",
    "        for i in range(k):\n",
    "            for j in ss:\n",
    "                temp+=int(j)\n",
    "            ss=str(temp)\n",
    "            temp=0\n",
    "        return int(ss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        t = ''\n",
    "        for i in range(len(s)):\n",
    "            tmp = ord(s[i]) - ord('a') + 1\n",
    "            t += str(tmp)\n",
    "        while k:\n",
    "            tmp = 0\n",
    "            for i in range(len(t)):\n",
    "                tmp += int(t[i])\n",
    "            t = str(tmp)\n",
    "            k -= 1\n",
    "        return int(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        digits = \"\".join(str(ord(ch) - ord(\"a\") + 1) for ch in s)\n",
    "        for i in range(k):\n",
    "            if len(digits)==1:\n",
    "                return int(digits)\n",
    "            digits=str(sum((int(i)for i in digits)))\n",
    "        return int(digits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        az = {\"a\":\"1\", \"b\":\"2\", \"c\":\"3\", \"d\":\"4\", \"e\":\"5\", \"f\":\"6\", \"g\":\"7\", \"h\":\"8\", \"i\":\"9\", \"j\":\"10\", \"k\":\"11\", \"l\":\"12\", \"m\":\"13\", \"n\":\"14\", \"o\":\"15\", \"p\":\"16\", \"q\":\"17\", \"r\":\"18\", \"s\":\"19\", \"t\":\"20\", \"u\":\"21\", \"v\":\"22\", \"w\":\"23\", \"x\":\"24\", \"y\":\"25\", \"z\":\"26\"}\n",
    "        t = \"\"\n",
    "        r = 0\n",
    "        for i in s:\n",
    "            t += az[i]\n",
    "        for j in range(k):\n",
    "            r = 0\n",
    "            for n in t:\n",
    "                r += int(n)\n",
    "            t = str(r)\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        s = list(s)\n",
    "        temp = \"\"\n",
    "        for i in s:\n",
    "            temp += str(ord(i)-ord(\"a\")+1)\n",
    "        temp = int(temp)\n",
    "        def calculate(num):\n",
    "            res = 0\n",
    "            while num>=10:\n",
    "                res += num%10\n",
    "                num = num //10\n",
    "            res += num\n",
    "            return res\n",
    "        print(temp)\n",
    "        for i in range(k):\n",
    "            temp = calculate(temp)\n",
    "        return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        digits = \"\".join(str(ord(ch) - ord(\"a\") + 1) for ch in s)\n",
    "\n",
    "        for i in range(k):\n",
    "            if len(digits) == 1:\n",
    "                break\n",
    "            total = sum(int(ch) for ch in digits)\n",
    "            digits = str(total)\n",
    "        \n",
    "        return int(digits)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        ans = 0\n",
    "        diff = ord('a') - 1\n",
    "        numstr = ''\n",
    "        for ch in s:\n",
    "            numstr += str(ord(ch) - diff)\n",
    "        for i in range(k, 0, -1):\n",
    "            ans = 0\n",
    "            for ch in numstr:\n",
    "                ans += int(ch)\n",
    "            numstr = str(ans)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        s = ''.join(str(ord(c) - ord('a') + 1) for c in s)\n",
    "        for _ in range(k):\n",
    "            t = sum(int(c) for c in s)\n",
    "            s = str(t)\n",
    "        return int(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        t=''\n",
    "        for i in s:\n",
    "            t+=str(ord(i)-96)\n",
    "        for j in range(k):\n",
    "            a = 0\n",
    "            for x in t:\n",
    "                a+=int(x)\n",
    "            t = str(a)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        s=''.join(str(ord(c)-ord('a')+1) for c in s)\n",
    "        for n in range(k):\n",
    "            t = sum(int(c) for c in s)\n",
    "            s=str(t)\n",
    "        return t\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        l = []\n",
    "        for c in s:\n",
    "            l.append(str(ord(c) - 96))\n",
    "        temp = ''.join(l)\n",
    "        for _ in range(k):\n",
    "            temp = str(eval('+'.join(temp)))\n",
    "        return int(temp)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "\n",
    "        dic={'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10,'k':11,'l':12,'m':13,'n':14,'o':15,'p':16,'q':17,'r':18,'s':19,'t':20,'u':21,'v':22,'w':'23','x':24,'y':25,'z':26}\n",
    "        t=0\n",
    "        cal=0\n",
    "        newstr=''\n",
    "        for i in s:\n",
    "            val=dic.get(i)\n",
    "            newstr=newstr+str(val)\n",
    " \n",
    "        while t<k:\n",
    "            sum=0\n",
    "            for j in newstr:\n",
    "                sum+=int(j)\n",
    "            cal=sum\n",
    "            newstr=str(sum)\n",
    "            t+=1\n",
    "        return cal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        sl=[str(ord(i)-96) for i in s]\n",
    "        si=[int(i) for i in ''.join(sl)]\n",
    "        print(si)\n",
    "        for i in range(k-1):\n",
    "            si=[int(i) for i in str(sum(si))]\n",
    "        return sum(si)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def getLucky(self, s: str, k: int) -> int:\r\n",
    "        alpha = list(chr(i) for i in range(97,123))\r\n",
    "        temp = str()\r\n",
    "        for i in s:\r\n",
    "            temp += str(alpha.index(i)+1)\r\n",
    "        count = 0\r\n",
    "        while count < k:\r\n",
    "            result = sum(int(j) for j in temp)\r\n",
    "            temp = str(result)\r\n",
    "            count += 1\r\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        digits = \"\".join(str(ord(ch) - ord(\"a\") + 1) for ch in s)\n",
    "\n",
    "        for i in range(k):\n",
    "            if len(digits) == 1:\n",
    "                break\n",
    "            total = sum(int(ch) for ch in digits)\n",
    "            digits = str(total)\n",
    "        \n",
    "        return int(digits)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        # 定义字母表映射\n",
    "        letter_map = {chr(i + ord('a')): i + 1 for i in range(26)}\n",
    "\n",
    "        # 转化为整数\n",
    "        integer_val = int(\"\".join(str(letter_map[char]) for char in s))\n",
    "\n",
    "        # 重复 k 次转换\n",
    "        for _ in range(k):\n",
    "            integer_val = sum(int(digit) for digit in str(integer_val))\n",
    "\n",
    "        return integer_val\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        s = ''.join(str(ord(c) - ord('a') + 1) for c in s)\n",
    "        for _ in range(k):\n",
    "            t = sum(int(c) for c in s)\n",
    "            s = str(t)\n",
    "        return int(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        oi = {}\n",
    "        re = 1\n",
    "        for i in range(97,123):\n",
    "            p = chr(i)\n",
    "            oi[p] = re\n",
    "            re += 1\n",
    "        ikun = ''\n",
    "        for we in s:\n",
    "            num = oi[we]\n",
    "            ikun += str(num)\n",
    "        for tr in range(k):\n",
    "            tr = sum(int(y) for y in ikun)\n",
    "            ikun = str(tr)\n",
    "        return int(ikun)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        ss=''\n",
    "        for i in s:\n",
    "            ss+=str(ord(i)-ord('a')+1)\n",
    "        \n",
    "        for _ in range(k):\n",
    "            ans=0\n",
    "            for j in ss:\n",
    "                ans+=int(j)\n",
    "            print(ans)\n",
    "            ss=str(ans)\n",
    "        else:\n",
    "            return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getLucky(self, s: str, k: int) -> int:\n",
    "        def transition(str_num):\n",
    "            res = 0\n",
    "            for i in str_num:\n",
    "                res+=int(i)\n",
    "            return res\n",
    "        str_num=''\n",
    "        for i in s:\n",
    "            str_num+=str(ord(i)-ord('a')+1)\n",
    "        for _ in range(k):\n",
    "            str_num = transition(str(str_num))\n",
    "        return str_num\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
